﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Media;

namespace Platformer
{
    class IceBolt
    {
        public Texture2D sprite;
        public Vector2 position;
        public float rotation;
        public Vector2 center;
        public Vector2 velocity;
        public bool alive, once = true;
        public Vector2 startPosition;
        
        private AnimationPlayer player;
        Level level;

        private Animation boltCollision;

        private SoundEffect shatter;

        public bool collided = false;

        public bool checkAlive
        {
            get { return alive; }
        }

        public void LoadContent()
        {
            boltCollision = new Animation(level.Content.Load<Texture2D>("Sprites/Player/boltCollision"), 0.1f, false);

            shatter = level.Content.Load<SoundEffect>("Sounds/IceCollision");
        }

        public Rectangle rectangle
        {
            get
            {
                int left = (int)position.X;
                int width = sprite.Width;
                int top = (int)position.Y;
                int height = sprite.Height;
                return new Rectangle(left, top, width, height);
            }
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (alive)
            {
                spriteBatch.Draw(sprite, position, Color.White);
            }

            if (collided)
                player.Draw(gameTime, spriteBatch, new Vector2(position.X, position.Y + 40), SpriteEffects.None);
        }

        public IceBolt(Level l, Vector2 pos, int dir)
        {
            this.level = l;
            rotation = 0.0f;
            
            position = new Vector2(pos.X, pos.Y - 40);
            startPosition = position;

            if (dir == 1)
            {
                velocity = new Vector2(-5.5f, 0.0f);
                sprite = level.Content.Load<Texture2D>("Sprites/Player/Freeze2");
            }
            else
            {
                velocity = new Vector2(5.5f, 0.0f);
                sprite = level.Content.Load<Texture2D>("Sprites/Player/Freeze1");
            }
            
            center = new Vector2(sprite.Width / 2, sprite.Height / 2);

            alive = true;
            LoadContent();
        }

        public void UpdateBolts()
        {
            //Only update them if they're alive
            if (alive)
            {

                //Move our bolt based on it's velocity
                position += velocity;

                //Rectangle the size of the screen so bolts that
                //fly off screen are deleted.
                
                Rectangle screenRect = new Rectangle(0, 0, 800, 600);
                if (!screenRect.Contains(new Point(
                    (int)position.X,
                    (int)position.Y)))
                {
                    alive = false;
                    collided = true;
                }
                

                //Sets maximum range to a certain number
                if (Vector2.Distance(startPosition, position) > 200)
                {
                    alive = false;
                    collided = true;
                }

                
                //Collision rectangle for each bolt -Will also be
                //used for collisions with enemies.
                Rectangle boltRect = new Rectangle(
                    (int)position.X,
                    (int)position.Y,
                    sprite.Width,
                    sprite.Height);

                //Check for collisions with the enemies
                foreach (Enemy enemy in level.enemies)
                {
                    if (boltRect.Intersects(enemy.BoundingRectangle))
                    {
                        // Module:             Games Programming
                        // Assignment1:        Platformer XNA Starter Kit
                        // Add game feature:   Enemy features
                        // Student Name:       Teo Zhi Wei Elvin
                        // Student Number:     S10077131J
                        // Class:              3T02

                        enemy.Health -= 15;
                        if (enemy.Health <= 0)

                        {
                            enemy.Alive = false;
                            level.Score += enemy.Points;
                        }

                        // Module:             Games Programming
                        // Assignment1:        Platformer XNA Starter Kit
                        // Add game feature:   Player features
                        // Student Name:       Kwong Jin Hao Ervin
                        // Student Number:     S10078631C
                        // Class:              3T02
                        //Immobilize here
                        enemy.freezeEnemy(1500);
                        alive = false;
                        collided = true;
                    }
                }

                
                //Look for adjacent tiles to the bolt
                Rectangle bounds = new Rectangle(
                    boltRect.Center.X,
                    boltRect.Center.Y,
                    boltRect.Width / 4,
                    boltRect.Height / 4);
                int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width);
                int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
                int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height);
                int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;

                // For each potentially colliding tile
                for (int y = topTile; y <= bottomTile; ++y)
                {
                    for (int x = leftTile; x <= rightTile; ++x)
                    {
                        TileCollision collision = level.GetCollision(x, y);

                        //If we collide with an Impassable or Platform tile
                        //then delete our bolt.
                        if (collision == TileCollision.Impassable ||
                            collision == TileCollision.Platform)
                        {
                            if (boltRect.Intersects(bounds))
                            {
                                alive = false;
                                collided = true;
                            }
                        }
                    }
                }
            }
            if (collided)
            {
                player.PlayAnimation(boltCollision);
                if (once == true)
                {
                    shatter.Play();
                    once = false;
                }
            }
        }
    }
}
